package org.jhulk.tests;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame; 
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.XMLPackedSheet;

public class MovementTest extends BasicGame{

	private Image marine;
	private float x;
	private float y;
	private int dist = 42;
	private float rot;
	private boolean ctrlPressed = false;
	
	public MovementTest() {
		super("JHulk movement tests");
	}

	@Override
	public void init(GameContainer container) 
		throws SlickException
	{
		x = 400;
		y = 300;
	}

	@Override
	public void render(GameContainer container, Graphics g) 
		throws SlickException
	{		
		try {
			XMLPackedSheet sheet = new XMLPackedSheet("data/img/packedTest.png", "data/img/packedTest.png.xml");
			marine = sheet.getSprite("marine_42.png");
			marine.rotate(rot);
			marine.draw(x,y);
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void update(GameContainer container, int delta) 
		throws SlickException {}
	
	public void keyPressed(int key, char c)
	{
		if(key == Input.KEY_LCONTROL || key == Input.KEY_RCONTROL) ctrlPressed = true;

		if(ctrlPressed)
		{
			if(key == Input.KEY_LEFT) rot -= 90;;

			if(key == Input.KEY_RIGHT) rot += 90; 
		}
		else
		{
			if(key == Input.KEY_LEFT) x -= dist;
	
			if(key == Input.KEY_RIGHT) x += dist; 
		}
		
		if(key == Input.KEY_UP) y -= dist;
		
		if(key == Input.KEY_DOWN) y += dist;
		
		if(key == Input.KEY_ESCAPE) System.exit(0);

	}

	public void keyReleased(int key, char c)
	{
		if(key == Input.KEY_LCONTROL || key == Input.KEY_RCONTROL) 
			ctrlPressed = false;
	}
	
	public static void main(String[] args)
	{
		try {
			AppGameContainer app = new AppGameContainer(new MovementTest());
			app.setDisplayMode(800, 600, false);
			app.start();
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}
}
